package org.streets.eis.component.buttonededit;

import org.apache.wicket.Response;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.streets.eis.component.WebResources;

public class ButtonedBehavior extends AbstractDefaultAjaxBehavior{

	private static final long serialVersionUID = 1L;


	public ButtonedBehavior()	{
	}
	
	@Override
	protected void onBind()	{
		this.getComponent().setOutputMarkupId(true);
	}

	@Override
	protected void onComponentRendered(){
		Response response = this.getComponent().getResponse();
		
		response.write("<img style=\"");
		response.write(getIconStyle());
		response.write("\"");
		response.write(" width=\"13px\" height=\"13px\"  ");
		response.write(" id=\"");
		response.write(this.getComponent().getMarkupId()+"_$Icon");
		response.write("\" src=\"");
		response.write(this.getComponent().getRequestCycle().urlFor(WebResources.Streets.Edit.BUTTONED_ICON));
		response.write("\" ");
		
		if (getComponent().isEnabled()){
			response.write(" title=\"点击选择\" ");
			response.write(" clickUrl=\"");
			response.write(this.getCallbackUrl());
			response.write("\" ");
		} else {
			response.write(" disabled=\"disabled\" " );			
		}

		response.write(" alt=\"选择\" ");
		response.write(" />");
	}

	protected String getIconStyle(){
		if (getComponent().isEnabled()){
			return "position:absolute;visibility:hidden;z-Index:1;cursor: pointer;";
		} else {
			return "position:absolute;visibility:hidden;z-Index:1";
		}
	}
	
	@Override
	protected void respond(AjaxRequestTarget target){
		onButtonClicked(target);		
	}
	
	protected void onButtonClicked(AjaxRequestTarget target){
	}
}
